<?php

require_once(APP_URL.'protected/myLib/msgPushLib/jPush/vendor/autoload.php');
use JPush\Model as M;
use JPush\JPushClient;
use JPush\Exception\APIConnectionException;
use JPush\Exception\APIRequestException;
define('JPUSH_APP_KEY', 'e6c45ac6c0e5dc2217c3eaae');
define('JPUSH_MASTER_SECRET', 'eb92b8dbf6fc73d7f214a38f');



/*
 * one class wraaper of JPush-Sdk
 */
class MsgUtil {
    
    var $TAG = 'Class MsgUtil';
    
    /**
     * 
     * @param unknown $userAliasArray
     * @param unknown $msgContent
     */
    public static function pushMsgByUserAlias($userAliasArray, $msgContent) {
        $client = new JPushClient(JPUSH_APP_KEY, JPUSH_MASTER_SECRET);
        try {
            $result = $client->push()
            ->setPlatform(M\platform('android', 'ios'))
            ->setAudience(M\audience(M\alias($userAliasArray)))
            ->setNotification(M\notification($msgContent))
            ->send();
        } catch (APIRequestException $e) {
            echo 'APIRequestException:  Push Failed...';
        } catch (APIConnectionException $e) {
            echo 'APIConnectionException:   Push Failed...';
        }
    }
    
    /**
     * 
     * @param unknown $videoTagArray
     * @param unknown $msgContent
     */
    public static function pushMsgByVideoTag($videoTagArray, $msgContent) {
        $client = new JPushClient(JPUSH_APP_KEY, JPUSH_MASTER_SECRET);
        try {
            $result = $client->push()
            ->setPlatform(M\platform('android', 'ios'))
            ->setAudience(M\audience(M\tag($videoTagArray)))
            ->setMessage(M\message($msgContent, null, null, null))
            ->send();
        } catch (APIRequestException $e) {
            echo 'APIRequestException:  Push Failed...';
        } catch (APIConnectionException $e) {
            echo 'APIConnectionException:   Push Failed...';
        }
    }
    
    /**
     * 
     * @param unknown $userAliasArray
     * @param unknown $notificationContent
     */
    public static function pushNotificationByUserAlias($userAliasArray, $notificationContent) {
        $client = new JPushClient(JPUSH_APP_KEY, JPUSH_MASTER_SECRET);
        try {
            $result = $client->push()
            ->setPlatform(M\platform('android', 'ios'))
            ->setAudience(M\audience(M\alias($userAliasArray)))
            ->setNotification(M\notification($notificationContent))
            ->send();
        } catch (APIRequestException $e) {
            echo 'APIRequestException:  Push Failed...';
        } catch (APIConnectionException $e) {
            echo 'APIConnectionException:   Push Failed...';
        }
    }
    
    /**
     * 
     * @param unknown $videoTagArray
     * @param unknown $notificationContent
     */
    public static function pushNotificationByVideoTag($videoTagArray, $notificationContent) {
        $client = new JPushClient(JPUSH_APP_KEY, JPUSH_MASTER_SECRET);
        try {
            $result = $client->push()
            ->setPlatform(M\platform('android', 'ios'))
            ->setAudience(M\audience(M\tag($videoTagArray)))
            ->setNotification(M\notification($notificationContent))
            ->send();
        } catch (APIRequestException $e) {
            echo 'APIRequestException:  Push Failed...';
        } catch (APIConnectionException $e) {
            echo 'APIConnectionException:   Push Failed...';
        }
    }
    
    
}
	